package org.example.yuaiagent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.yuaiagent.chatmemory.MysqlChatMemory;
import org.example.yuaiagent.common.Result;
import org.example.yuaiagent.model.entity.ConversationRecord;
import org.example.yuaiagent.service.ConversationRecordService;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/message_memory")
public class MessageMemoryController {
    @Autowired
    private MysqlChatMemory mysqlChatMemory;

    @Autowired
    private ConversationRecordService conversationRecordService;
    /**
     * 得到指定对话id的最新N条消息
     * @param conversationId
     * @param lastN
     * @return
     */
    @GetMapping("/getByID")
    public Result get(String conversationId, int lastN){
        List<Message> messageList = mysqlChatMemory.get(conversationId, lastN);
        return Result.ok("获取成功", messageList);
    }
    /**
     * 分页查询指定聊天记录上下文
     * @param conversationId
     * @param message
     * @return
     */
    @PostMapping("/getContextByPage")
    public Result getContextByPage(String conversationId, int pageNum, int pageSize){
        List<Message> messageList = mysqlChatMemory.getContextByPage(conversationId, pageNum, pageSize);
        return Result.ok("获取成功", messageList);
    }
    /**
     * 新建一个聊天对话
     * @param conversationId
     * @param messages
     */
    @GetMapping("/create")
    public Result create(String userId,Integer type){
        //参数校验
        if (userId == null || userId.length() == 0) {
            return Result.fail("用户id不能为空", null);
        }
        if (type == null) {
            return Result.fail("对话类型不能为空", null);
        }
        String id = UUID.randomUUID().toString();
        ConversationRecord conversationRecord = new ConversationRecord();
        conversationRecord.setId(id);
        conversationRecord.setUserId(userId);
        conversationRecord.setType(type);
        conversationRecord.setTitle("新对话"+id.substring(0,8));
        conversationRecordService.save(conversationRecord);
        return Result.ok("创建成功", id);
    }
    /**
     * 分页显示对话记录
     * @param userId
     * @param type 0:普通对话，1智能体对话
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result list(String userId,Integer type ,int pageNum, int pageSize){
        LambdaQueryWrapper<ConversationRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConversationRecord::getUserId, userId)
                .eq(ConversationRecord::getType, type)
                .orderByDesc(ConversationRecord::getCreateTime)
                .last("LIMIT " + (pageNum-1)*pageSize + "," + pageSize);
        List<ConversationRecord> list = conversationRecordService.list(queryWrapper);
        return Result.ok("获取成功", list);
    }

    /**
     * 删除一个聊天对话
     * @param conversationId
     */
    @GetMapping("/delete")
    public Result delete(String conversationId){
        ConversationRecord conversationRecord = conversationRecordService.getById(conversationId);
        if (conversationRecord == null) {
            return Result.fail("删除失败,对话不存在", null);
        }
        conversationRecordService.removeById(conversationId);
        mysqlChatMemory.clear(conversationId);
        return Result.ok("删除成功", "");
    }
}
